package com.iemes.controller.warehouse;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.EnterstorageFormMap;
import com.iemes.entity.ReckonformFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.warehouse.EnterstorageMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.MyDateUtils;

@RequestMapping("/monthreckon_form/")
@Controller
public class Monthreckon_FormController extends BaseController {
	
	@Inject
	private EnterstorageMapper enterstorageMapper;
	
	// 查看入库报表，显示按钮
	@RequestMapping("monthreckon_form")
	public String monthreckon_form(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/warehouse/monthreckon_form_list";
	}
	
	// 入库报表分页
	@ResponseBody
	@RequestMapping("monthreckon_form_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		EnterstorageFormMap enterstorageFormMap = getFormMap(EnterstorageFormMap.class);
		List<Map<String, Object>> enter = enterstorageMapper.getAll(enterstorageFormMap);
		LuogicPaging(pageNow, pageSize, enter);
		return pageView;
	}
	
	// 编辑月度盘点表
	@RequestMapping("monthreckon_form_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id获得入库报表
			Map<String, Object> enter=enterstorageMapper.getById(id);
			model.addAttribute("enter",enter);
			//获得相同enterId的月度盘点表
			ReckonformFormMap reckonformFormMap=new ReckonformFormMap();
			reckonformFormMap.put("enterId", enter.get("id"));
			List<ReckonformFormMap> reckon=baseMapper.findByNames(reckonformFormMap);
			model.addAttribute("reckon",reckon);
		}
		return Common.BACKGROUND_PATH + "/warehouse/monthreckon_form_edit";
	}
	
	// 修改月度盘点表
	@ResponseBody
	@RequestMapping(value = "monthreckon_form_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "月度盘点表", methods = "编辑月度盘点表-修改月度盘点表") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws NumberFormatException, Exception {
		EnterstorageFormMap enterstorageFormMap=getFormMap(EnterstorageFormMap.class);
		//先删除
		baseMapper.deleteByAttribute("enterId","'"+Integer.parseInt(enterstorageFormMap.getStr("id"))+"'", ReckonformFormMap.class);
		//新增标签子项
		int enterId=Integer.parseInt(request.getParameter("enterstorageFormMap.id"));
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		String[] count=request.getParameterValues("count_1");
		String[] remark=request.getParameterValues("remark_1");
		String[] time=request.getParameterValues("time_1");
		if(count!=null){
			for(int i = 0 ;i<count.length;i++){
				ReckonformFormMap reckonformFormMap=new ReckonformFormMap();
				reckonformFormMap.put("enterId", enterId);
				reckonformFormMap.put("count", count[i]);
				reckonformFormMap.put("remark", remark[i]);
				reckonformFormMap.put("time", time[i]);
				reckonformFormMap.put("reckoningPeopleId", userFormMap.getInt("id"));
				baseMapper.addEntity(reckonformFormMap);
				enterstorageFormMap.put("restCount", count[count.length-1]);
				baseMapper.editEntity(enterstorageFormMap);
			}
		}
		return "success";
	}
}








